﻿//用到的命名空间 
using System;
using System.Web;
using System.Xml;


namespace msBsc.Cls
{ 
    //继承自IHttpModule接口 
    public static class SaveRequestFile 
    {  
        public static void doRequest(HttpContext context)
        {
            string requestPath = context.Request.RawUrl;
            string strNewfilepath = requestPath.Replace("/Caf/", "").Replace("/", "\\").TrimStart(new char[] { '\\', ' ' });
            strNewfilepath = CurrAppPath + strNewfilepath;
            if (System.IO.File.Exists(strNewfilepath))
            {
                return;
            }


            string sterrrr = "";
            if (requestPath.StartsWith("http"))
            {
                sterrrr = requestPath;
                strNewfilepath = CurrAppPath + "Caf\\Home\\" + requestPath.Substring(requestPath.LastIndexOf("/")+1);
            }
            else
            {
                sterrrr = "http://themepixels.com/demo/webpage/chain/" + requestPath.Replace("/Caf/Home/", "");
            }

            if (strNewfilepath.IndexOf("?") != -1)
            {
                strNewfilepath = strNewfilepath.Substring(0, strNewfilepath.IndexOf("?"));
            }
            msBsc.SvRef.SRWebClient oSRWebClient = new msBsc.SvRef.SRWebClient();
            oSRWebClient.DownloadFile(sterrrr, strNewfilepath, false);
            //System.IO.FileInfo ofinfo2 = new System.IO.FileInfo(strfilepath);
            //if (!ofinfo2.Exists)
            //{
            //    continue;
            //}

             
        }


        //public string MergeResourceAndCompress(string strquery)
        //{
        //    System.Text.StringBuilder sbRet = new System.Text.StringBuilder();
        //    string[] arrstrteem = strquery.Split(',');
        //    int icount = arrstrteem.Length;
        //    for (int i = 0; i < arrstrteem.Length; i++)
        //    {
        //        string strfilepath = arrstrteem[i];
        //        //if (strfilepath.StartsWith("j_"))
        //        //{
        //        strfilepath = CurrAppPath + "jqPlugin\\" + strfilepath.Replace("/", "\\");
        //        //}
        //        msBsc.AppLogic.Cfg.JSCompress oJSCompress = new AppLogic.Cfg.JSCompress();
        //        sbRet.AppendLine(oJSCompress.doCompressFromFile(strfilepath));
        //    }

        //    return sbRet.ToString();
        //}


        private static string _CurrAppPath = "";
        /// <summary>
        /// 程序主目录,WEB站点目录，以\\结尾
        /// </summary>
        public static string CurrAppPath
        {
            get
            {
                if (_CurrAppPath == string.Empty)
                {
                    if (HttpContext.Current == null)
                    {
                        _CurrAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    }
                    else
                    {
                        _CurrAppPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
                    }
                    if (!_CurrAppPath.EndsWith("\\"))
                    {
                        _CurrAppPath = _CurrAppPath + "\\";
                    }
                }
                return _CurrAppPath;
            }
        }



    }
}